//
//  NetTool.m
//  YiChat
//
//  Created by Hesilen on 16/7/7.
//  Copyright © 2016年 Hesilen. All rights reserved.
//

#import "NetTool.h"

static MBProgressHUD *hud = nil;


@implementation NetTool


+ (AFHTTPSessionManager *)getSessionManager
{
    
    
    
    [self showIndicator];
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    return mgr;
}

+ (void)showIndicator
{
    
    if (!hud)
    {
        hud = [MBProgressHUD showHUDAddedTo:WINDOW animated:YES];
    }
    
    [hud show:YES];
}

+ (void)hideIndicator
{
    if (hud)
    {
        [hud hide:YES];
    }
}
+ (void)startMonitoring
{
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    //监听网络
    [mgr startMonitoring];
    
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"网络状态未知!");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"不可联网!");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"wwan");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"wifi");
                break;
                
            default:
                break;
        }
    }];
}


//要想获取到当前网络状态，必须先开启网络监听状态。而网络监听又需要消耗一定的时间来执行，所以，一般情况下，我们监听网络状态都是在appdelegate里面去开启监听的
+ (BOOL)checkNetwork
{
    
    // [self startMonitoring];
    
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    
    NSLog(@"当前网络状态---%@",mgr.localizedNetworkReachabilityStatusString);
    
    return  mgr.reachable;
}

@end
